<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Client extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('client_model');
    }

    function index() {
        $data['clients'] = $this->client_model->getList();

        $this->layout
                ->title('Clients')
                ->js('functions')
                ->js('controllers/client')
                ->js('controllers/adresse')
                ->view('client/index', $data);
    }

    public function table() {
        $result = $this->client_model->getList();
        return json_encode($result);
    }

    /*     * *************************************************************************
     * EDIT
     * ************************************************************************ */

    // Create
    public function create() {
        $data['gestionnaires'] = $this->client_model->getListUtil();
        $this->load->view('client/edit', $data);
    }

    public function createDB() {
        $SOCIETE_NOM = $this->input->get('SOCIETE_NOM');
        $UTILISATEUR_ID = $this->input->get('UTILISATEUR_ID');
        if ($UTILISATEUR_ID == "null")
            $UTILISATEUR_ID = null;
        $result = $this->client_model->create($SOCIETE_NOM, $UTILISATEUR_ID);
        echo $this->table();
    }

    // Update
    public function update() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
   
        $data['client'] = $this->client_model->get($SOCIETE_ID);
        $data['gestionnaires'] = $this->client_model->getListUtil();
        
        $this->load->view('client/edit', $data);
    }

    public function updateDB() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        $SOCIETE_NOM = $this->input->get('SOCIETE_NOM');
        $UTILISATEUR_ID = $this->input->get('UTILISATEUR_ID');
        if ($UTILISATEUR_ID == "null")
            $UTILISATEUR_ID = null;
        $this->client_model->update($SOCIETE_ID, $SOCIETE_NOM, $UTILISATEUR_ID);
        echo $this->table();
    }

    // Delete
    public function delete() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        $this->client_model->delete($SOCIETE_ID);
        echo $this->table();
    }

}